package qjc.h_二叉树;

import com.二叉树.练习题.TreeNode;
import com.链表.练习题.ListNode;

public class h5_二叉树的直径 {
    /**
     * 递归遍历左右节点加一取最大值
     * @param root
     * @return
     */
    public int diameterOfBinaryTree(TreeNode root) {
        depth(root);
        return ans;
    }

    private int depth(TreeNode root) {
        if(root==null){
            return -1;
        }
        int depth1 =depth(root.left)+1;
        int depth2=depth(root.right)+1;
        ans=Math.max((depth1+depth2),ans);
        return Math.max(depth1,depth2);
    }

    int ans = 0;
}
